package vip.xiaonuo.biz.modular.lancerShopCart.service.impl;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.biz.modular.lancerGoods.service.BizGoodsService;
import vip.xiaonuo.biz.modular.lancerGoodsBox.service.BizGoodsBoxService;
import vip.xiaonuo.biz.modular.lancerUserGoods.entity.BizUserGoods;
import vip.xiaonuo.common.pojo.DbEnum;
import vip.xiaonuo.biz.modular.pojo.domain.BizGoods;
import vip.xiaonuo.biz.modular.pojo.domain.BizGoodsBox;
import vip.xiaonuo.biz.modular.pojo.domain.BizShopCartItem;
import vip.xiaonuo.biz.modular.lancerShopCart.service.BizShopCartItemService;
import vip.xiaonuo.biz.modular.mapper.BizShopCartItemMapper;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.modular.pojo.dto.add.ShopCartItemAddDTO;
import vip.xiaonuo.biz.modular.pojo.dto.common.CommonDeleteDTO;
import vip.xiaonuo.biz.modular.pojo.dto.common.CommonDetailDTO;
import vip.xiaonuo.biz.modular.pojo.dto.edit.ShopCartItemEditDTO;
import vip.xiaonuo.biz.modular.pojo.dto.page.ShopCartItemPageDTO;
import vip.xiaonuo.common.page.CommonPageRequest;

import java.util.List;

/**
 * @author LancerAdmin
 * @description 针对表【biz_shop_cart_item(购物车)】的数据库操作Service实现
 * @createDate 2024-02-29 14:54:24
 */
@Service
public class BizShopCartItemServiceImpl extends ServiceImpl<BizShopCartItemMapper, BizShopCartItem>
        implements BizShopCartItemService {

    @Override
    public Page<BizShopCartItem> pageLAdmin(ShopCartItemPageDTO dto) {
        LambdaQueryWrapper<BizShopCartItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(ObjectUtil.isNotEmpty(dto.getUserName()),
                        BizShopCartItem::getUserName,
                        dto.getUserName())
                .like(ObjectUtil.isNotEmpty(dto.getGoodsName()),
                        BizShopCartItem::getGoodsName,
                        dto.getGoodsName())
                .like(ObjectUtil.isNotEmpty(dto.getGoodsBoxName()),
                        BizShopCartItem::getGoodsBoxName,
                        dto.getGoodsBoxName())
                .orderByDesc(BizShopCartItem::getCreateTime);

        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }
    @Override
    public Page<BizShopCartItem> pageClient(ShopCartItemPageDTO dto) {
        LambdaQueryWrapper<BizShopCartItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(BizShopCartItem::getUserId,StpLoginUserUtil.getLoginUser().getId())
                .like(ObjectUtil.isNotEmpty(dto.getGoodsName()),
                        BizShopCartItem::getGoodsName,
                        dto.getGoodsName())
                .like(ObjectUtil.isNotEmpty(dto.getGoodsBoxName()),
                        BizShopCartItem::getGoodsBoxName,
                        dto.getGoodsBoxName())
                .orderByDesc(BizShopCartItem::getCreateTime);

        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public void deleteClient(String id) {
        this.removeById(id);
    }

    @Override
    public BizShopCartItem detail(CommonDetailDTO dto) {
        return this.getById(dto.getId());
    }

    @Autowired
    BizGoodsBoxService bizGoodsBoxService;

    @Autowired
    BizGoodsService goodsService;


    @Override
    public void add(ShopCartItemAddDTO dto) {
        String goodsId = dto.getGoodsId();
        BizGoodsBox bizGoodsBox = bizGoodsBoxService.getObjByGoodsId(goodsId);
        BizGoods bizGoods = goodsService.getById(goodsId);
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        // 准备数据
        String goodsBoxName = bizGoodsBox.getGoodsBoxName();
        String goodsBoxId = bizGoodsBox.getId();
        Integer count = dto.getCount();
        String goodsName = bizGoods.getGoodsName();
        Integer goodsPriceFee = bizGoods.getGoodsPriceFee();
        String name = loginUser.getName();
        String userId = loginUser.getId();

        BizShopCartItem obj = new BizShopCartItem();
        obj
                .setGoodsId(goodsId)
                .setCount(count)
                .setGoodsBoxId(goodsBoxId)
                .setGoodsBoxName(goodsBoxName)
                .setGoodsName(goodsName)
                .setGoodsOriginPrice(goodsPriceFee)
                .setUserId(userId)
                .setUserName(name)
                .setSelectStatus(DbEnum.YOrN.NO.getValue());
        this.save(obj);
    }

    @Override
    @Transactional
    public void delete(List<CommonDeleteDTO> dto) {
        List<String> idList = CollStreamUtil.toList(dto, CommonDeleteDTO::getId);
        // 删除类型
        this.removeBatchByIds(idList);
    }

    @Override
    public void edit(ShopCartItemEditDTO dto) {
        String id = dto.getId();

        String selectStatus = dto.getSelectStatus();
        DbEnum.YOrN.validate(selectStatus);
        LambdaUpdateWrapper<BizShopCartItem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(BizShopCartItem::getSelectStatus, selectStatus)
                .eq(BizShopCartItem::getId, id);
        this.update(updateWrapper);
    }

    @Override
    public void addGoods(String id,Integer count) {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        BizGoodsBox box = bizGoodsBoxService.getObjByGoodsId(id);
        BizShopCartItem shopCartItem = new BizShopCartItem();
        // 盒子
        shopCartItem
                .setGoodsBoxName(box.getGoodsBoxName())
                .setGoodsBoxId(box.getId());
        // 商品
        BizGoods bizGoods = goodsService.getById(id);
        shopCartItem.setGoodsId(id)
                .setGoodsName(bizGoods.getGoodsName())
                .setGoodsOriginPrice(bizGoods.getGoodsPriceFee())
        // 消费者信息
                .setUserId(loginUser.getId())
                .setUserName(loginUser.getName())
                .setSelectStatus(DbEnum.YOrN.NO.getValue());
        // 数量
        shopCartItem.setCount(count);
        // 提交
        this.save(shopCartItem);
    }


}




